<?php
namespace ac\controllers;

use Yii;
use ac\controllers\BasicController;
use app\models\Admin;
use common\component\tool\Tool;
use common\component\tool\ValidateCode;
use common\component\exception\ErrMap;
use common\models\OperationLog;
use common\models\Areas;


class PublicController extends BasicController
{
    public $layout = false;

	/*登录*/
    public function actionLogin()
    {
        if(Yii::$app->request->isPost){
            /*查看验证码*/
            $vaildateCode = $this->session->get('vaildate_code');
            // P($vaildateCode);
            if(empty($vaildateCode) or (strtolower($vaildateCode) != strtolower($this->post["Admin"]["checkcode"]))){
                $this->session->remove('vaildate_code');
                list($errno, $errmsg) = ErrMap::getError(10008);
                $exception = '';
            }else{
                $this->post = Yii::$app->request->post();
                // P($this->post);
                $admin = new Admin;
                if($admin->login($this->post)){
                    $userInfo = Yii::$app->session->get('loginInfo')->userInfo;
                    // P($userInfo);
                    $args = array(
                        'loginid' => $userInfo['admin_id'],
                        'loginname' => $userInfo['username'],
                        'back_code' => 0,
                        'back_info' => '登录成功',
                        'comid' => $userInfo['comid']
                    );
                    /*返回前台的登录数据*/
                    $uinfo = array(
                        'username' => $userInfo['username'],
                        'comid' => $userInfo['comid'],
                        'avatar' => $userInfo['avatar'],
                        'identity' => $userInfo['identity'],
                        'issuper' => $userInfo['issuper'],
                        'level' => $userInfo['level'],
                        // 'permissions' => Yii::$app->session->get('loginInfo')->permissions,
                    );
                    if(gettype($res = OperationLog::addLog($args, '', $uinfo)) != 'boolean'){//操作日志
                        return $res;
                    };
                }else{
                    if($admin->hasErrors()){
                        // P($admin->getErrors());
                        list($errno, $errmsg) = ErrMap::getError(10009);
                        $exception = $admin->getErrors();
                        if(array_key_exists("accountDisable", $exception)){
                            list($errno, $errmsg) = ErrMap::getError(10005, '该账户未启用');//返回信息
                        };
                        if(array_key_exists("enterpriseDisable", $exception)){
                            list($errno, $errmsg) = ErrMap::getError(10005, '该企业未启用');//返回信息
                        };
                        if(array_key_exists("weekDisable", $exception)){
                            list($errno, $errmsg) = ErrMap::getError(10005, '周'. date("w") .'不允许登录');//返回信息
                        };
                    }
                }
            }
            $args = array(
                'loginid' => 0,
                'loginname' => isset($this->post['Admin']['username'])?$this->post['Admin']['username']:'',
                'back_code' => $errno,
                'back_info' => $errmsg,
            );
            if(gettype($res = OperationLog::addLog($args, $exception)) != 'boolean'){//操作日志
                return $res;
            };

        };
        list($errno, $errmsg) = ErrMap::getError(10005);
        $args = array(
            'loginid' => 0,
            'loginname' => '',
            'back_code' => $errno,
            'back_info' => $errmsg,
        );
        if(gettype($res = OperationLog::addLog($args)) != 'boolean'){//操作日志
            return $res;
        };
    }

    /*登出*/
    public function actionLogout()
    {
        Yii::$app->session->removeAll();
        if(Yii::$app->session->get('loginInfo') === null){
            return Tool::showRes([0, '成功登出']);
        }else{
            return Tool::showRes(ErrMap::getError(10006));
        }
    }


    /*验证码*/
    public function actionCreateCode()
    {
        Yii::$app->response->send();//防止报错 Headers already sent in......
        $session = Yii::$app->session;
        $session -> remove('vaildate_code');
        $code = new ValidateCode();
        $code -> doimg();
        $session -> set('vaildate_code', $code -> getCode());
    }

    /*取出所有省数据*/
    public function actionGetProvinceList()
    {
        $list = Areas::getProvinceList();
        return Tool::showRes([0, '获取成功'], $list);
    }

    /*取出 市 数据 根据省ID*/
    public function actionGetCityByProvince()
    {
        $provinceid = $this->get['id'];
        $list = [];
        if($provinceid){
            $list = Areas::getCityByProvince($provinceid);
        }
        return Tool::showRes([0, '获取成功'], $list);
    }

    /*取出 区 数据 根据市ID*/
    public function actionGetDistrictByCity()
    {
        $cityid = $this->get['id'];
        $list = [];
        if($cityid){
            $list = Areas::getDistrictByCity($cityid);
        }
        return Tool::showRes([0, '获取成功'], $list);
    }

    /*百度编辑器验证*/
    public function actionUeditor()
    {
        return '{}';
    }

    
}
